package org.example.pack1209;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class DemoB {

    private static void a() throws Exception {
        Scanner scan = new Scanner(System.in);

        System.out.print("请输入姓名：");
        String ename = scan.next();
        System.out.print("请输入职位：");
        String job = scan.next();
        System.out.print("请输入上司ID：");
        int mgr = scan.nextInt();
        System.out.print("请输入工资：");
        double sal = scan.nextDouble();
        System.out.print("请输入奖金：");
        double comm = scan.nextDouble();
        System.out.print("请输入部门ID：");
        int deptNo = scan.nextInt();

        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/2411";
        String user = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, user, password);
        String sql = "insert into emp values(null,'" + ename + "','" + job + "'," + mgr + ",now()," + sal + "," + comm + "," + deptNo + ",1)";
        PreparedStatement ps = conn.prepareStatement(sql);
        int row = ps.executeUpdate();
        System.out.println(row + "行受到影响");
        ps.close();
        conn.close();
    }

    private static void b() throws Exception {
        Scanner scan = new Scanner(System.in);

        System.out.print("请输入姓名：");
        String ename = scan.next();
        System.out.print("请输入职位：");
        String job = scan.next();
        System.out.print("请输入上司ID：");
        int mgr = scan.nextInt();
        System.out.print("请输入工资：");
        double sal = scan.nextDouble();
        System.out.print("请输入奖金：");
        double comm = scan.nextDouble();
        System.out.print("请输入部门ID：");
        int deptNo = scan.nextInt();

        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/2411";
        String user = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, user, password);
        // ? 代表的是参数，是一个占位符
        String sql = "insert into emp values(null,?,?,?,now(),?,?,?,1)";
        PreparedStatement ps = conn.prepareStatement(sql);
        // 在这个位置给?赋值
        ps.setString(1, ename);
        ps.setString(2, job);
        ps.setInt(3, mgr);
        ps.setDouble(4, sal);
        ps.setDouble(5, comm);
        ps.setInt(6, deptNo);
        int row = ps.executeUpdate();
        System.out.println(row + "行受到影响");
        ps.close();
        conn.close();
    }

    public static void main(String[] args) throws Exception {
        a();
    }

}